using System;

namespace BanZuiCloudControlAgent.Utils.Database.Models
{
    /// <summary>
    /// 消息状态枚举
    /// </summary>
    public enum MessageStatus
    {
        /// <summary>
        /// 处理中
        /// </summary>
        Processing = 0,
        
        /// <summary>
        /// 处理完成
        /// </summary>
        Completed = 1,
        
        /// <summary>
        /// 处理失败
        /// </summary>
        Failed = 2
    }
    
    /// <summary>
    /// MQTT消息数据对象，对应数据库中的MqttMessages表
    /// </summary>
    public class MqttMessageDO
    {
        /// <summary>
        /// 自增主键
        /// </summary>
        public long Id { get; set; }
        
        /// <summary>
        /// 消息ID，全局唯一
        /// </summary>
        public string MessageId { get; set; }
        
        /// <summary>
        /// 消息类型
        /// </summary>
        public string MessageType { get; set; }
        
        /// <summary>
        /// 时间戳
        /// </summary>
        public string Timestamp { get; set; }
        
        /// <summary>
        /// 设备码
        /// </summary>
        public string DeviceCode { get; set; }
        
        /// <summary>
        /// 消息负载（JSON字符串）
        /// </summary>
        public string Payload { get; set; }
        
        /// <summary>
        /// 处理时间
        /// </summary>
        public string ProcessedTime { get; set; }
        
        /// <summary>
        /// 消息状态（0:处理中, 1:处理完成, 2:处理失败）
        /// </summary>
        public int Status { get; set; }
        
        /// <summary>
        /// 失败次数
        /// </summary>
        public int FailCount { get; set; }
        
        /// <summary>
        /// 最后处理时间
        /// </summary>
        public string LastProcessTime { get; set; }
        
        /// <summary>
        /// 创建时间
        /// </summary>
        public string CreateTime { get; set; }
        
        /// <summary>
        /// 默认构造函数
        /// </summary>
        public MqttMessageDO()
        {
            Status = (int)MessageStatus.Processing;
            FailCount = 0;
            LastProcessTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff");
            CreateTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff");
        }
        
        /// <summary>
        /// 从BaseMessageModel创建MqttMessageDO
        /// </summary>
        /// <param name="baseMessage">基础消息模型</param>
        /// <param name="payloadJson">负载JSON字符串</param>
        /// <returns>MQTT消息数据对象</returns>
        public static MqttMessageDO FromBaseMessage(Core.Mqtt.Models.BaseMessageModel baseMessage, string payloadJson)
        {
            if (baseMessage == null)
                return null;
                
            return new MqttMessageDO
            {
                MessageId = baseMessage.MessageId,
                MessageType = baseMessage.MessageType,
                Timestamp = baseMessage.Timestamp,
                DeviceCode = baseMessage.DeviceCode,
                Payload = payloadJson,
                Status = (int)MessageStatus.Processing,
                FailCount = 0,
                LastProcessTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff")
            };
        }
    }
} 